﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using wxt;

namespace Workplatm
{
    /// <summary>
    /// Interaction logic for Window_ChoseDMMC.xaml
    /// </summary>
    public partial class Window_ChoseDMMC : Window
    {
        String searchText = "";
        int[] fj = null;
        DataSet ds = null;
        String resultInfo = "";
        public String dm = "";
        public String mc = "";
        public String dmmc = "";
        public bool selectedData = false;

        public Window_ChoseDMMC(String sqlFjfa, char charFj, String sqlData,String searchText)  
        {
            InitializeComponent();
            this.searchText = searchText;
            this.text_search.TxtValue = this.searchText;
            try
            {
                intData(sqlFjfa, charFj, sqlData);
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
        }
        public Window_ChoseDMMC(String sqlFjfa, char charFj, String sqlData, String searchText,Double top,Double left)
        {
            InitializeComponent();
            this.searchText = searchText;
            this.text_search.TxtValue = this.searchText;
            try
            {
                intData(sqlFjfa, charFj, sqlData);
                this.WindowStartupLocation = WindowStartupLocation.Manual;
                this.Top = top-this.ActualHeight/2;
                this.Left = left-this.ActualWidth/2;
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
            
        }
        //初始化数据
        private void intData(String sqlFjfa, char charFj, String sqlData)
        {
            //获取代码分级方案 
            DataSet dsfj =Dao.getSql(sqlFjfa, "GL_FZXLB", out resultInfo);
            if (dsfj != null && dsfj.Tables.Count > 0)
            {
                foreach (DataRow row in dsfj.Tables[0].Rows)
                {
                    String lbfj = row[0].ToString();
                    String[] fjTemp = lbfj.Split(charFj);
                    fj = new int[fjTemp.Length];
                    for (int i = 0; i < fjTemp.Length; i++)
                    {
                        fj[i] = Int16.Parse(fjTemp[i]);
                    }
                }
            }
            String rInfo = "";
            //填充信息
            this.ds = Dao.getSql(sqlData, "gl_xmzl", out rInfo);
            resetData(this.ds);
            
        } 
        private void resetData(DataSet ds ) {
            this.treeView_dmmc.Items.Clear();
            if (ds != null && ds.Tables.Count > 0)
            {
                foreach (DataRow row in ds.Tables[0].Rows)
                {
                    int preLen = 0;
                    String preDM = "";
                    String xmdm = row[0].ToString();
                    String xmmc = row[1].ToString();
                    for (int i = 0; (i < fj.Length && preLen < xmdm.Length); i++)
                    {
                        if ((preLen + fj[i]) >= xmdm.Length)
                        {
                            break;
                        }
                        else
                        {
                            preLen += fj[i];
                        }
                    }
                    preDM = xmdm.Substring(0, preLen);
                    TreeViewItem tvi = new TreeViewItem();
                    tvi.Name = "name_" + xmdm;
                    tvi.Header = xmdm + "-" + xmmc;
                    tvi.MouseDoubleClick += Tvi_MouseDoubleClick;
                    Boolean hasPre = false;
                    ItemCollection itemConllection = this.treeView_dmmc.Items;
                    loopTreeViewNode(itemConllection, tvi, preDM, fj);
                }
            }
        }
        //双击节点名称事件
        private void Tvi_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            TreeViewItem tvi = this.treeView_dmmc.SelectedItem as TreeViewItem;
            if (this.treeView_dmmc.SelectedItem != null  && !tvi.HasItems)
            {
                this.dm = tvi.Name.Substring(5);
                this.mc = tvi.Header.ToString();
                this.mc.Substring(this.mc.IndexOf("-") >= 0 ? this.mc.IndexOf("-") : 0);
                this.dmmc = tvi.Header.ToString();
                this.selectedData = true;
                this.Close();
            }
        }

        //循环查询节点
        private Boolean loopTreeViewNode(ItemCollection itemConllection,TreeViewItem node,String preDM,int [] fj)
        {
            Boolean hasPre = false;
            try
            {
                foreach (TreeViewItem tviTemp in itemConllection)
                {
                    if (tviTemp.Name.Equals("name_" + preDM))
                    { 
                        tviTemp.Items.Add(node);
                        hasPre = true;
                        break;
                    }
                    else if (node.Name.Substring(0,tviTemp.Name.Length).Equals(tviTemp.Name) && tviTemp.HasItems && !hasPre)
                    {
                        hasPre = loopTreeViewNode(tviTemp.Items, node, preDM, fj);
                    }
                    else
                    {
                        continue;
                    }
                }
                if (!hasPre)
                { 
                    this.treeView_dmmc.Items.Add(node);
                }
                return hasPre;
            }
            catch (Exception e1)
            {  
                return hasPre;
            }
        }
           //搜索按钮事件
        private void btn_search_Click(object sender, RoutedEventArgs e)
        { 
            String searchText = this.text_search.TxtValue;
            if (searchText.Equals("")) { 
                resetData(this.ds);
                return;
            }  
            DataSet dsTemp = this.ds.Copy();
            dsTemp.Clear();
            foreach (DataRow row in  this.ds.Tables[0].Rows) {
                String dm = row[0].ToString();
                String mc = row[1].ToString();
                if (dm.IndexOf(searchText) == 0 || mc.IndexOf(searchText) >= 0 || (dm + "-" + mc).IndexOf(searchText) >= 0) { 
                    dsTemp.Tables[0].Rows.Add(dm,mc);
                }
            }
            resetData(dsTemp);
        }
        //确认按钮点击返回
        private void btn_selected_Click(object sender, RoutedEventArgs e)
        {
            if (this.treeView_dmmc.SelectedItem != null) {

                this.dm = (this.treeView_dmmc.SelectedItem as TreeViewItem).Name.Substring(5);
                this.mc = (this.treeView_dmmc.SelectedItem as TreeViewItem).Header.ToString();
                this.mc.Substring(this.mc.IndexOf("-")>=0?this.mc.IndexOf("-"):0);
                this.dmmc = (this.treeView_dmmc.SelectedItem as TreeViewItem).Header.ToString();
                this.selectedData = true;
                this.Close();
            }
        }

        private void text_search_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key.Equals(Key.Enter))
            {
                String searchText = this.text_search.TxtValue;
                if (searchText.Equals(""))
                {
                    resetData(this.ds);
                    return;
                }
                DataSet dsTemp = this.ds.Copy();
                dsTemp.Clear();
                foreach (DataRow row in this.ds.Tables[0].Rows)
                {
                    String dm = row[0].ToString();
                    String mc = row[1].ToString();
                    if (dm.IndexOf(searchText) >= 0 || mc.IndexOf(searchText) >= 0 || (dm + "-" + mc).IndexOf(searchText) >= 0)
                    { 
                        dsTemp.Tables[0].Rows.Add(dm, mc);
                    }
                }
                resetData(dsTemp);
            }
        }
        //取消按钮事件
        private void btn_cancel_Click(object sender, RoutedEventArgs e)
        {
            this.selectedData = false;
            this.Close();
        }
    }
}
